//
//  B_AddBankViewController.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/18.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_AddBankViewController.h"
#import "B_ReleaseFooterView.h"
#import "B_AddBankTVC.h"
#import "B_AddBankModel.h"

@interface B_AddBankViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) BaseTableView * mainView;

@property (nonatomic, strong) B_ReleaseFooterView * addBankFooter;

@end

@implementation B_AddBankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"添加银行卡"];
    
    
    B_AddBankModel * model0 = [B_AddBankModel initB_AddBankModelWithTitle:@"银行卡号" placeholder:@"请输入银行卡号" mapword:@"bank_number" alert:@"请输入银行卡号!"];
    B_AddBankModel * model1 = [B_AddBankModel initB_AddBankModelWithTitle:@"身份证号" placeholder:@"请输入身份证号" mapword:@"id_number" alert:@"请输入身份证号!"];
    B_AddBankModel * model2 = [B_AddBankModel initB_AddBankModelWithTitle:@"姓名" placeholder:@"请输入姓名" mapword:@"name" alert:@"请输入姓名"];
    B_AddBankModel * model3 = [B_AddBankModel initB_AddBankModelWithTitle:@"手机号" placeholder:@"请输入手机号" mapword:@"phone_number" alert:@"请输入手机号!"];
    
    self.dataArray = [NSMutableArray arrayWithObjects:model0, model1, model2, model3, nil];
    self.view = self.mainView;
    
}


- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView addSubview:self.addBankFooter];
        [self.addBankFooter mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left);
            make.right.equalTo(self.mainView.mas_right);
            make.bottom.equalTo(self.mainView.mas_bottom);
            make.height.equalTo(@(80));
        }];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (80 + 24) : 80;
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:-bottom];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_AddBankTVC" bundle:nil] forCellReuseIdentifier:@"B_AddBankTVC"];
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_AddBankModel * model = weSelf.dataArray[indexPath.row];
            B_AddBankTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_AddBankTVC" forIndexPath:indexPath];
            cell.B_keyLabel.text = model.key_title;
            cell.B_imputTextField.placeholder = model.placeholder_name;
            cell.B_imputTextField.delegate = weSelf;
            cell.B_imputTextField.tag = indexPath.row;
            cell.B_imputTextField.text = model.value_name;
            if ([model.map_word isEqualToString:@"bank_number"] || [model.map_word isEqualToString:@"phone_number"]) {
                cell.B_imputTextField.keyboardType = UIKeyboardTypeNumberPad;
            } else {
                cell.B_imputTextField.keyboardType = UIKeyboardTypeDefault;
            }
            return cell;
            
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 90;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            
        };
        
    }
    return _mainView;
}

- (B_ReleaseFooterView *)addBankFooter {
    if (!_addBankFooter) {
        WeakSelf(weSelf, self);
        _addBankFooter = [B_ReleaseFooterView initB_ReleaseFooterView];
        [_addBankFooter.B_SureButton setTitle:@"确认绑定" forState:UIControlStateNormal];
        _addBankFooter.sureActionBlock = ^{
            //TODO:执行绑定操作
            [weSelf submit];
        };
    }
    return _addBankFooter;
}

- (void) submit {
    
    // http://detectionBankCard.api.juhe.cn/bankCard?key=0dd7ec2924d01a8dd479f8cf778b92fb&cardid=
    
    
    NSMutableDictionary * dicitonary = [NSMutableDictionary dictionary];
    for (B_AddBankModel * model in self.dataArray) {
        if (model.value_name == nil || [model.value_name isEqualToString:@""]) {
            [UIView xg_toast:model.alert_message];
            return;
        }
        if ([model.map_word isEqualToString:@"id_number"]) {
            if (![NSString accurateVerifyIDCardNumber:model.value_name]) {
                [UIView xg_toast:@"请输入正确的身份证号!"];
                return;
            } else {
                [dicitonary setObject:model.value_name forKey:@"userIdCard"];
            }
        } else if ([model.map_word isEqualToString:@"phone_number"]) {
            if (![model.value_name isMobileNumber]) {
                [UIView xg_toast:@"请输入正确手机号码!"];
                return;
            } else {
                [dicitonary setObject:model.value_name forKey:@"userBankTel"];
            }
        } else if ([model.map_word isEqualToString:@"bank_number"]) {
            [dicitonary setObject:model.value_name forKey:@"userBankCard"];
        } else if ([model.map_word isEqualToString:@"name"]) {
            [dicitonary setObject:model.value_name forKey:@"username"];
        }
    }
    /// TOOD:执行上传
    
    NSString * urlString = [NSString stringWithFormat:@"http://detectionBankCard.api.juhe.cn/bankCard?key=0dd7ec2924d01a8dd479f8cf778b92fb&cardid=%@", dicitonary[@"bank_number"]];
    WeakSelf(weSelf,  self);
    [NetworkingManager getNetworkRequestWithUrlString:urlString parameters:nil isCache:NO succeed:^(id data) {
        if ([[NSString stringWithFormat:@"%d", data[@"error_code"]] isEqualToString:@"0"]) {
            if ([data[@"result"][@"nature"] isEqualToString:@"借记卡"]) {
                [dicitonary setObject:data[@"result"][@"bank"] forKey:@"userBankType"];
                
                [NetworkingManager getNetworkRequestWithUrlString:@"/yearnfor/inter/bankCard/userBankCard" parameters:dicitonary isCache:NO succeed:^(id data) {
                    if ([data[@"code"] isEqualToString:@"20022"]) {
                        [weSelf.navigationController popViewControllerAnimated:YES];
                    } else {
                        [KeyWindow makeToast:data[@"message"]];
                    }
                } fail:^(NSError *error) {
                    [KeyWindow makeToast:@"服务器异常,请稍后重试!"];
                }];
                
            } else {
                [KeyWindow makeToast:@"请输入借记卡卡号!"];
            }
        } else {
            [KeyWindow makeToast:data[@"reason"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常,请稍后重试!"];
    }];
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    B_AddBankModel * model = self.dataArray[textField.tag];
    model.value_name = textField.text;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
